import _ from 'lodash'
import store from '../store'
import axios from '@/utils/axios'
import { Message } from 'element-ui'

const MODAL = {
  CONFIRM: 'CONFIRM',
  SEND_GOODS: 'SEND_GOODS',
  PREVIEW_CAROUSEL: 'PREVIEW_CAROUSEL',
  SHOWPWD: 'SHOWPWD'
}

const initialState = {
  confirm: {
    show: false,
    title: '',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    isSaveBtn: false,
    paragraph: '',
    showCancel: true,
    confirmed: 0 // 0 取消，1 不存储，2 存储
  },
  sendGoods: {
    show: false,
    order: {}
  },
  previewCarousel: {
    list: [],
    show: false
  },
  showpwd: false
}

const mutations = {
  [MODAL.CONFIRM] (state, payload) {
    state.confirm = {
      ...state.confirm,
      ...payload
    }
  },
  [MODAL.SEND_GOODS] (state, payload) {
    state.sendGoods = {
      ...state.sendGoods,
      ...payload
    }
  },
  [MODAL.PREVIEW_CAROUSEL] (state, payload) {
    state.previewCarousel = {
      ...state.previewCarousel,
      ...payload
    }
  },
  [MODAL.SHOWPWD] (state, payload) {
    console.log(payload)
    state.showpwd = payload
  }
}


const actions = {
  changeConfirm ({ commit }, payload) {
    commit(MODAL.CONFIRM, payload)
  },
  changeShowpwd ({ commit }, payload) {
    commit(MODAL.SHOWPWD, payload)
  },
  showConfirmModal({ commit, dispatch, state }, { title, confirmButtonText, isSaveBtn, paragraph, showCancel = true }) {
    return new Promise((resolve, reject) => {
      dispatch('changeConfirm', {
        show: true,
        title,
        confirmButtonText,
        isSaveBtn,
        paragraph,
        showCancel,
        confirmed: 0
      })
      const unwatch = store.watch(function (state, getters) {
        return state.modal.confirm.show
      }, function () {
        unwatch()
        resolve(state.confirm.confirmed)
      })
    })
  },
  changeSendGoods ({ commit }, payload) {
    commit(MODAL.SEND_GOODS, payload)
  },
  changeCarousel ({ commit }, payload) {
    commit(MODAL.PREVIEW_CAROUSEL, payload)
  },
}

const getters = {}

export default {
  state: _.cloneDeep(initialState),
  mutations,
  actions,
  getters
}
